require "common"

--判断字符串
function judge_str( num , max)
        local out = ""
        local st = {}
        if num == nil then return nil end
        local len = string.len( num )

        for i=1,len, 2 do
                c = string.sub(num,i,i+1)
                if tonumber(c) > max or tonumber(c) < 1 then return nil end

        end
        return true
end
function sort_str( num )
	num = string.gsub( num, "*", "")
	local out = ""
	local st = {}
	local len = string.len( num )
	for i=1,len, 2 do
		table.insert( st, string.sub(num,i,i + 1) )
	end
	table.sort(st)
	for i=1,len/2 do
		out = out..st[i]
	end

	return out, st

end
--判断字符串是否在min max区间
function between( num , min, max)
        local out = ""
        local st = {}
        if num == nil then return nil end
        local len = string.len( num )

        for i=1,len, 2 do
                c = string.sub(num,i,i+1)
                if tonumber(c) > max or tonumber(c) < min then return nil end

        end
        return true
end
--同花顺计算
function tonghuashun(huase, djStr)
	djStr = sort_str(djStr)
	if huase == 1 and      string.find('011213#01020304050607080910111213', djStr) then
		return true
	elseif huase == 2 and string.find('142526#14151617181920212223242526', djStr) then
		return true
	elseif huase == 3 and  string.find('273839#27282930313233343536373839', djStr) then
		return true
  elseif huase == 4 and  string.find('405152#40414243444546474849505152', djStr) then
		return true
	else
		return nil
	end
	return nil
end

--包选
function baoxuan(type, djStr)
	local betNum = {}
	for i = 1, string.len(djStr), 2 do
		betNum[(i+1)/2] = tonumber(string.sub(djStr,i,i+1))
	end
	if type == 'tonghua' then
		for i=1, 4 do
			if between(djStr, (i-1)*13+1, i*13) then
				return true
			end
		end
	elseif type == 'tonghuashun' then
		djStr = format_num(betNum[1])..format_num(betNum[2])..format_num(betNum[3])
		djStr = sort_str(djStr)
		for i=1, 4 do
			if tonghuashun(i, djStr) then
				return true
			end
		end
	elseif type == 'shunzi' then
		for i = 1, #betNum do
			betNum[i] = (betNum[i]-1)%13 + 1
		end
		djStr = format_num(betNum[1])..format_num(betNum[2])..format_num(betNum[3])
		djStr = sort_str(djStr)
		if string.find('011213#01020304050607080910111213', djStr) then
			return true
		end
	elseif type == 'baozi' then
		for i = 1, #betNum do
			betNum[i] = (betNum[i]-1)%13 + 1
		end
		djStr = format_num(betNum[1])..format_num(betNum[2])..format_num(betNum[3])
		djStr = sort_str(djStr)
		_,distNum = get_distinct_num(djStr)
		if distNum == 1 then
			return true
		end
	elseif type == 'duizi' then
		for i = 1, #betNum do
			betNum[i] = (betNum[i]-1)%13 + 1
		end
		djStr = format_num(betNum[1])..format_num(betNum[2])..format_num(betNum[3])
		djStr = sort_str(djStr)
		_,distNum = get_distinct_num(djStr)
		if distNum == 2 then
			return true
		end
	end

	return nil
end

--获取字符串中唯一的字符个数

function get_distinct_num( num )
	local dist = {}
	local str = {}
	local out = ""
	local len = string.len( num )
	str = sort_str(num)
	for i=1,len ,2 do
		c = string.sub(str,i,i+1)
		if not in_table( c, dist ) then
			table.insert( dist, c )
		end
	end
	for i = 1,table.getn(dist) do
		out = out..dist[i]
	end
	return	out , table.getn(dist)

end

function check_pk3(butType, inStr)
	local butNum = 0
	local butPower = 1
	local itemList = {}
	local itemLen = 0
	local nTmp = 0

	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )
	if itemList[1] == '' then return 0 end

	if itemLen > 2 or itemLen < 1  then return 0 end
	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end



	--任选1--6单式
	if butType == '1' or butType == '2' or butType == '3' or butType == '4' or butType == '5' or butType == '6' then
		jItem = Split(itemList[1], "#")
		if #jItem ~=1 then return 0 end
		if  not judge_str(itemList[1],13) then return 0 end
		if string.len(itemList[1])/2 ~= tonumber(butType) then return 0 end
		--inStr = sort_str(itemList[1]).."#B"..butPower
		--不能有重复的号码
		_, distNum = get_distinct_num(itemList[1])
		nNum = string.len(itemList[1])/2
		if distNum ~= nNum then return 0 end
		butNum = 1

	--任选1--6复式
	elseif butType == '7' or butType == '8' or butType == '9' or butType == 'a' or butType == 'b' or butType == 'c' then

		if butType == '7' or butType == '8' or butType == '9' then
			butType = tonumber(butType) - 6
		else
			butType = string.byte(butType) - string.byte('a') + 4
		end
		jItem = Split(itemList[1], "#")
		nNum = string.len(itemList[1])/2
		if #jItem ~=1 then return 0 end
		if  not judge_str(itemList[1],13) then return 0 end
		if nNum > 13 or nNum <= butType then return 0 end
		--不能有重复的号码
		_, distNum = get_distinct_num(itemList[1])
		if distNum ~= nNum then return 0 end

		butNum = combination(nNum, butType)
	--任选2-6胆拖
	elseif butType == 'd' or butType == 'e' or butType == 'f' or butType == 'g' or butType == 'h' then
		butType = string.byte(butType) - string.byte('d') + 2
		jItem = Split(itemList[1], "#")
		nNumD = string.len(jItem[1])/2
		nNumT = string.len(jItem[2])/2

		if #jItem ~= 2 or  not judge_str(jItem[1],13) or not judge_str(jItem[2],13) then return 0 end
		if nNumD >= butType or nNumD + nNumT <= butType then return 0 end
		--不能有重复的号码
		_, distNum = get_distinct_num(jItem[1]..jItem[2])
		if distNum ~= nNumD + nNumT then return 0 end
		butNum = combination(nNumT, butType - nNumD)
	--同花单选单式
	elseif butType == 'i' then
		jItem = Split(itemList[1], "#")
		if #jItem > 4 or #jItem < 1 then return 0 end
		if not judge_str(string.gsub(itemList[1],"#", ""), 4) then return 0 end
		for i=1,#jItem do
			if string.len(jItem[i]) / 2 ~= 1 then
				return 0
			end
		end
		butNum = #jItem
	--同花顺单选单式
	elseif butType == 'j' then
		jItem = Split(itemList[1], "#")
		if #jItem > 4 or #jItem < 1 then return 0 end
		if not judge_str(string.gsub(itemList[1],"#", ""), 4) then return 0 end
		for i=1,#jItem do
			if string.len(jItem[i]) / 2 ~= 1 then
				return 0
			end
		end
		butNum = #jItem
	--顺子单式
	elseif butType == 'k' then
		str = "0102030405060708091011121301"
		jItem = Split(itemList[1], "#")
		if #jItem ~=1 then return 0 end
		if  not judge_str(itemList[1],13) then return 0 end
		if string.find(str, jItem[1]) == nil or string.len(jItem[1])/2 ~= 3 then return 0 end

		butNum = 1
	--豹子单式
	elseif butType == 'l' then
		jItem = Split(itemList[1], "#")
		if #jItem ~=1 then return 0 end
		if  not judge_str(itemList[1],13) then return 0 end
		if string.len(jItem[1])/2 ~= 3 then return 0 end
		_, distNum = get_distinct_num(jItem[1])
		if distNum ~= 1 then return 0 end
		butNum = 1
	--对子单式
	elseif butType == 'm' then
		jItem = Split(itemList[1], "#")
		if #jItem ~=1 then return 0 end
		if  not judge_str(itemList[1],13) then return 0 end
		if string.len(jItem[1])/2 ~= 2 then return 0 end
		_, distNum = get_distinct_num(jItem[1])
		if distNum ~= 1 then return 0 end
		butNum = 1
	--对子复式
	elseif butType == 'n' then
		jItem = Split(itemList[1], "#")
		if #jItem < 2 or #jItem > 13 then return 0 end
		for i = 1, #jItem do
			if  not judge_str(jItem[i],13) then return 0 end
			if string.len(jItem[i])/2 ~= 2 then return 0 end
			_, distNum = get_distinct_num(jItem[i])
			if distNum ~= 1 then return 0 end
		end
		butNum = #jItem
	--豹子复式
	elseif butType == 'o' then
		jItem = Split(itemList[1], "#")
		if #jItem < 2 or #jItem > 13 then return 0 end
		for i = 1, #jItem do
			if  not judge_str(jItem[i],13) then return 0 end
			if string.len(jItem[i])/2 ~= 3 then return 0 end
			_, distNum = get_distinct_num(jItem[i])
			if distNum ~= 1 then return 0 end
		end
		butNum = #jItem
	--顺子顺子复式
	elseif butType == 'p' then
		str = "0102030405060708091011121301"
		jItem = Split(itemList[1], "#")
		if #jItem < 2 or #jItem > 12 then return 0 end
		for i = 1, #jItem do
			if  not judge_str(jItem[i],13) then return 0 end
			if string.find(str, jItem[i]) == nil or string.len(jItem[i])/2 ~= 3 then return 0 end
		end

		butNum = #jItem
	--包选单式
	elseif butType == 'q' then
		jItem = Split(itemList[1], "#")
		if #jItem ~= 1 then return 0 end
		if not judge_str(jItem[1], 5) then return 0 end
		if string.len(jItem[1]) / 2 ~= 1 then
			return 0
		end
		butNum = 1
	else
		return 0
	end
	return butNum, butPower, inStr, inStr

end

function query_pk3(butType,queryStr,djStr)
--奖级 ：任选1-6  7同花 8同花顺 9顺子 10豹子 11对子 12同花包选 13同花顺包选  14顺子包选 15豹子包选 16对子包选
	local nBingo = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
	local temp 	 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
	local butPower = 1
	local bingNum = 0
	local bingoD = 0
	local bingoT = 0
	local bullStr = {}

	itemList = Split( queryStr, "#B" )
	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end

	for i = 1, string.len(djStr), 2 do
		bullStr[(i+1)/2] = tonumber(string.sub(djStr,i,i+1))
	end
	--任选1-6
	if butType == '1' or butType == '2' or butType == '3' or butType == '4' or butType == '5' or butType == '6'
	or butType == '7' or butType == '8' or butType == '9' or butType == 'a' or butType == 'b' or butType == 'c' then
		if butType >= '1' and butType <= '6' then
			butType = tonumber(butType)
		elseif butType == '7' or butType == '8' or butType == '9' then
			butType = tonumber(butType) - 6
		else
			butType = string.byte(butType) - string.byte('a') + 4
		end
		for i = 1, #bullStr do
			bullStr[i] = (bullStr[i]-1)%13 + 1
		end
		djStr = format_num(bullStr[1])..format_num(bullStr[2])..format_num(bullStr[3])
		djStr,distNum = get_distinct_num(djStr)
		bingoNum = ConputeBingoNum(djStr, itemList[1], 2)
		if(butType >= 3 and bingoNum == distNum) then
			nBingo[butType] = combination(string.len(itemList[1])/2 - distNum, butType -distNum)
		else
			nBingo[butType] = combination(bingoNum, butType)
		end
	--同花单选单式
	elseif butType == 'i' then
		jItem = Split( itemList[1], "#" )
		for i = 1, #jItem do
			--黑桃
			if tonumber(jItem[i]) == 1 and between(djStr, 1, 13) then
				nBingo[7] = nBingo[7] + 1
			--红桃
			elseif tonumber(jItem[i]) == 2 and between(djStr, 14, 26) then
				nBingo[7] = nBingo[7] + 1
			--梅花
			elseif tonumber(jItem[i]) == 3 and between(djStr, 27, 39) then
				nBingo[7] = nBingo[7] + 1
			--方块
			elseif tonumber(jItem[i]) == 4 and between(djStr, 40, 52) then
				nBingo[7] = nBingo[7] + 1
			end
		end
	--同花顺单选单式
	elseif butType == 'j' then
		jItem = Split( itemList[1], "#" )
		for i = 1, #jItem do
			--黑桃
			if tonumber(jItem[i]) == 1 and tonghuashun(1, djStr) then
				nBingo[8] = nBingo[8] + 1
			--红桃
			elseif tonumber(jItem[i]) == 2 and tonghuashun(2, djStr) then
				nBingo[8] = nBingo[8] + 1
			--梅花
			elseif tonumber(jItem[i]) == 3 and tonghuashun(3, djStr) then
				nBingo[8] = nBingo[8] + 1
			--方块
			elseif tonumber(jItem[i]) == 4 and tonghuashun(4, djStr) then
				nBingo[8] = nBingo[8] + 1
			end
		end
	--顺子单式
	elseif butType == 'k' or butType == 'p' then
		jItem = Split( itemList[1], "#" )
		for i = 1, #bullStr do
			bullStr[i] = (bullStr[i]-1)%13 + 1
		end
		djStr = format_num(bullStr[1])..format_num(bullStr[2])..format_num(bullStr[3])
		djStr = sort_str(djStr)
		for i = 1, #jItem do
			jItem[i] = sort_str(jItem[i])
			if jItem[i] == djStr then
				nBingo[9] = nBingo[9] + 1
			end
		end
		butNum = 1
	--豹子
	elseif butType == 'l' or butType == 'o' then
		jItem = Split( itemList[1], "#" )
		for i = 1, #bullStr do
			bullStr[i] = (bullStr[i]-1)%13 + 1
		end
		djStr = format_num(bullStr[1])..format_num(bullStr[2])..format_num(bullStr[3])
		for i = 1, #jItem do
			if jItem[i] == djStr then
				nBingo[10] = nBingo[10] + 1
			end
		end
		butNum = 1
	--对子
	elseif butType == 'm' or butType == 'n' then
		jItem = Split( itemList[1], "#" )
		for i = 1, #bullStr do
			bullStr[i] = (bullStr[i]-1)%13 + 1
		end
		djStr = format_num(bullStr[1])..format_num(bullStr[2])..format_num(bullStr[3])
		djStr = sort_str(djStr)
		_,distNum = get_distinct_num(djStr)
		for i = 1, #jItem do
			if (string.sub(djStr, 1, 4) == jItem[i] or string.sub(djStr, 3, 6) == jItem[i]) and distNum == 2 then
				nBingo[11] = nBingo[11] + 1
			end
		end
		butNum = 1
	--包选单式
	elseif butType == 'q' then
		jItem = Split(itemList[1], "#")
		--同花包选
		if tonumber(jItem[1]) == 1 and baoxuan('tonghua', djStr) then
			nBingo[12] =  1
		--同花顺包选
		elseif tonumber(jItem[1]) == 2 and baoxuan('tonghuashun', djStr) then
			nBingo[13] =  1
		--顺子
		elseif tonumber(jItem[1]) == 3 and baoxuan('shunzi', djStr) then
			nBingo[14] =  1
		--豹子
		elseif tonumber(jItem[1]) == 4 and baoxuan('baozi', djStr) then
			nBingo[15] =  1
		--对子
		elseif tonumber(jItem[1]) == 5 and baoxuan('duizi', djStr) then
			nBingo[16] =  1
		end
	else
		return 0
	end

	for i=1 ,16 do
		nBingo[i] = nBingo[i] * butPower
	end
	return nBingo[1]..","..nBingo[2]..","..nBingo[3]..","..nBingo[4]..","..nBingo[5]..","..nBingo[6]..","..nBingo[7]..","..nBingo[8]..","..nBingo[9]..","..nBingo[10]..","..nBingo[11]..","..nBingo[12]..","..nBingo[13]..","..nBingo[14]..","..nBingo[15]..","..nBingo[16]



end
--print('1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6')
--print(query_pk3('q',"02#B1", "082223"))
--print(check_pk3('p',"01#B2"))
--print(check_k3('9',"12#2"))
--print(check_k3('b',"123456#2"))
--print(query_k3('9',"16#4", "010105"))
